package yannan.voiplock4ble.service.ble;

import android.content.Context;

import org.greenrobot.greendao.annotation.NotNull;

import yannan.voiplock4ble.base.MyApplication;
import yannan.voiplock4ble.bean.LockBean;
import yannan.voiplock4ble.bean.LockKeyBean;
import yannan.voiplock4ble.utils.PopShowUtils;

/**
 * 说明：门锁操作的类 删除 修改 等
 * 作者：mainTel
 * 时间：2016/12/7 10:19
 * 备注：
 */
public class BleOperUtils {


    /**
     * 启用禁用 密码 指静脉等
     *
     * @param bean
     * @param mContext
     * @param bleService
     */
    public static void banPickKey(final LockKeyBean bean, Context mContext, final BleService bleService) {
        final String isBan = bean.isBan;
        String msg1 = "";
        int operType = -1;
        if (bean.type.equals("0")) {
            if (isBan.equals("0")) {
                msg1 = "确定要启用指静脉钥匙" + bean.userIdentifier + "么？";
                operType = 1;
            } else {
                msg1 = "确定要禁用指静脉钥匙" + bean.userIdentifier + "么？";
                operType = 0;
            }
            final int finalOperType1 = operType;
            PopShowUtils.showCommonDialog(mContext, msg1
                    , "取消", "确定", true
                    , new PopShowUtils.IDialogCommonListener() {
                        @Override
                        public void onClick(int direction) {
                            if (direction == PopShowUtils.COMMON_DIALOG_RIGHT)
                                // TODO: 2016/12/1 发送修改命令 如果成功则 修改用户 并上传告知后台信息
                                bleService.sendOrder(BleUtils.operFingerUser(
                                        MyApplication.getInterface().mConnectLock.contentToken,
                                        bean.userIdentifier.getBytes(),
                                        finalOperType1, Integer.parseInt(bean.templateGroupId), Integer.parseInt(bean.templateId)
                                ));
                        }
                    });
        } else {
            if (isBan.equals("0")) {
                msg1 = "确定要启用密码钥匙" + bean.userIdentifier + "么？";
                operType = 1;
            } else {
                msg1 = "确定要禁用密码钥匙" + bean.userIdentifier + "么？";
                operType = 0;
            }
            final int finalOperType = operType;
            PopShowUtils.showCommonDialog(mContext, msg1
                    , "取消", "确定", true
                    , new PopShowUtils.IDialogCommonListener() {
                        @Override
                        public void onClick(int direction) {
                            if (direction == PopShowUtils.COMMON_DIALOG_RIGHT)
                                bleService.sendOrder(BleUtils.operPwdUser(
                                        MyApplication.getInterface().mConnectLock.contentToken,
                                        Integer.parseInt(bean.userIdentifier),
                                        finalOperType
                                ));
                        }
                    });
        }
    }


    /**
     * 设置管理员
     *
     * @param bean
     * @param mContext
     * @param bleService
     */
    public static void editAdmin(final LockKeyBean bean, Context mContext, final BleService bleService) {
        final String userPermission = bean.userPermission;
        String msg = "";
        int operType = -1;
        if (userPermission.equals("0")) {
            msg = "确定要将" + bean.userIdentifier + "设置为管理员么？";
            operType = 0;
        } else {
            msg = "确定取消" + bean.userIdentifier + "的管理员权限么？";
            operType = 1;
        }
        final int finalOperType = operType;
        PopShowUtils.showCommonDialog(mContext, msg
                , "取消", "确定", true
                , new PopShowUtils.IDialogCommonListener() {
                    @Override
                    public void onClick(int direction) {
                        if (direction == PopShowUtils.COMMON_DIALOG_RIGHT)
                            bleService.sendOrder(BleUtils.operFingerAdminUser(
                                    MyApplication.getInterface().mConnectLock.contentToken,
                                    bean.userIdentifier.getBytes(),
                                    finalOperType,
                                    Integer.parseInt(bean.templateGroupId), Integer.parseInt(bean.templateId)
                            ));
                    }
                });
    }

    /**
     * 检查门锁是够链接
     *
     * @param lockBean
     * @return
     */
    public static boolean checkBleConnect(@NotNull LockBean lockBean) {

        if (lockBean == null) {
            return false;
        }
        if (MyApplication.getInterface().mConnectLock == null) {
            return false;
        }
        if (MyApplication.getInterface().mConnectLock.contentToken == 0) {
            return false;
        }
        if (!lockBean.lockserialId.equals(MyApplication.getInterface().mConnectLock.lockserialId)) {
            return false;
        }
        return true;
    }

    public static boolean checkBleConnect() {
        if (MyApplication.getInterface().mConnectLock == null) {
            return false;
        }
        if (MyApplication.getInterface().mConnectLock.contentToken == 0) {
            return false;
        }
        return true;
    }


    /**
     * 删除指静脉
     *
     * @param bean
     * @param mContext
     * @param bleService
     */
    public static void delKey(final LockKeyBean bean, Context mContext, final BleService bleService) {
        if (bean.type.equals("0")) { //指静脉
            PopShowUtils.showCommonDialog(mContext, "确定要删除指静脉" + bean.userIdentifier + "么？"
                    , "取消", "确定", true
                    , new PopShowUtils.IDialogCommonListener() {
                        @Override
                        public void onClick(int direction) {
                            // TODO: 2016/12/2 发送命令给门锁，如果门锁返回成功，则进行删除
                            if (direction == PopShowUtils.COMMON_DIALOG_RIGHT)
                                bleService.sendOrder(
                                        BleUtils.delFingerUser(MyApplication.getInterface()
                                                        .mConnectLock.contentToken,
                                                bean.userIdentifier.getBytes(), Integer.parseInt(bean.templateId), Integer.parseInt(bean.templateGroupId))
                                );
                        }
                    });
        } else {  //密码
            PopShowUtils.showCommonDialog(mContext, "确定要删除密码" + bean.userIdentifier + "么？"
                    , "取消", "确定", true
                    , new PopShowUtils.IDialogCommonListener() {
                        @Override
                        public void onClick(int direction) {
                            // TODO: 2016/12/2 发送命令给门锁，如果门锁返回成功，则进行删除
                            if (direction == PopShowUtils.COMMON_DIALOG_RIGHT)
                                bleService.sendOrder(
                                        BleUtils.deletePwdUser(MyApplication.getInterface()
                                                        .mConnectLock.contentToken,
                                                Integer.parseInt(bean.userIdentifier))
                                );
                        }
//                        NetUtils.editLockKey(bean, NetUtils.EDITLOCKUSERINFO_DEL, new NetUtils.IOnEditLockKeyCallBack() {
//                            @Override
//                            public void onSuc(CommonResponse resBody) {
//                                if (resBody.returnValue.equals("1")) {
//                                    List<LockKeyBean> lists =
//                                            DaoUtils.getDao(LockKeyDaoManager.class)
//                                                    .getQueryBuilder()
//                                                    .where(LockKeyBeanDao.Properties.LockserialId.eq(bean.getLockserialId()),
//                                                            LockKeyBeanDao.Properties.UserIdentifier.eq(bean.getUserIdentifier()))
//                                                    .build().list();
//                                    if (lists.size() != 0)
//                                        DaoUtils.getDao(LockKeyDaoManager.class).delete(lists.get(0));
//                                    adapter.notifyItemRemoved(poi);
//                                }
//                            }
//
//                            @Override
//                            public void onFail(String msg) {
//                            }
//                        });
                    });
        }
    }
}
